<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>商户迁移</title>
    <link rel="stylesheet" href="../../../../bangnuo/admin/layui/css/layui.css">
</head>

<body>

<div class="layui-form" lay-filter="layuiadmin-form-useradmin" id="layuiadmin-form-useradmin"
     style="padding: 20px 0 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">上级代理</label>
        <div class="layui-input-block" style="width: 200px;">
            <select name="newAgentId" id="newAgentId">
            </select>
        </div>
    </div>



    <div class="layui-btn-container" style="padding-bottom: 10px; margin-left: 110px; margin-right: -110px;">
        <button class="layui-btn" lay-submit lay-filter="admin-form" data-type="">立即提交</button>
    </div>
</div>
</body>
<script src="../../../../bangnuo/admin/layui/layui.js"></script>


<script>
    //配置插件目录
    layui.config({
        base: '../../../../bangnuo/admin/'

    });
    //一般直接写在一个js文件中
    layui.use(['layer', 'form', 'layarea'], function () {
        var layer = layui.layer
            , form = layui.form
            , layarea = layui.layarea;
        layarea.render({
            elem: '#area-picker',
            change: function (res) {
                //选择结果
                console.log(res);
            }
        });
    });
</script>
<script src="../../../../bangnuo/admin/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
    layui.use(['form', 'upload', 'layer'], function () {
        var form = layui.form;
        $(document).ready(function () {
            //获取地址栏路径
            var url = window.location.href;
            //获取“=”的下标
            var pos = url.indexOf("=");
            //获取“=”后的全部字符（用户id）
            var id = url.substring(pos + 1, url.length);
            var htmls = '<option value="">请选择</option>'; //全局变量
            $.ajax({
                url: "/agentEntity/findAll",
                contentType: "application/json;charset=UTF-8",
                type: "GET",
                success: function (res) {
                    if(res.success){
                        var resultData = res.object;
                        for(var i=0;i<res.object.length;i++){
                            htmls += '<option value = "' + resultData[i].id + '">' + resultData[i].companyName + '</option>';
                        }
                        $("#newAgentId").html(htmls);
                        form.render('select');
                    }

                }
            });
        });
    });
</script>
<!--获取表单信息，提交表单-->
<script type="text/javascript">

    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(admin-form)', function () {
            //layer.msg(JSON.stringify(data.field));

            var newAgentId=$("#newAgentId").val();

            console.log("newAgentId",newAgentId);

            var idsStr = getQueryVariable("ids");
            if(idsStr != null && idsStr !=undefined){
                var ids = idsStr.split(",");
                var datas = [];
                for(var i=0;i<ids.length;i++){
                    var data = {
                        commercialTenantId: ids[i],
                        newAgentId:$("#newAgentId").val()
                    }
                    datas.push(data);
                }
                $.ajax({
                    type: "POST",
                    url: "/commercialMigration/migrateList",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(datas),
                    async: true,
                    success: function (res) {
                        if (res.success == true) {
                            layer.msg(data.message);
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        } else {
                            layer.msg(data.message);
                        }
                    }
                });
            }else{
                var id = getQueryVariable("id");
                var data = {
                    commercialTenantId: id,
                    newAgentId:$("#newAgentId").val()
                }
                $.ajax({
                    type: "POST",
                    url: "/commercialMigration/migrate",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(data),
                    async: true,
                    success: function (res) {
                        if (res.success == true) {
                            layer.msg(data.message);
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        } else {
                            layer.msg(data.message);
                        }
                    }
                });
            }


            return false;
        });
    });
</script>
<!--关闭弹层-->
<script>
    layui.use('layer', function () {
        var $ = layui.$,
            layer = layui.layer,
            index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        var active = {
            close: function () {
                var id = '#LAY_layer_iframe_demo',
                    mark = $('#LAY_mark');
                parent.layer.msg('修改成功');
                parent.layer.tips('Look here', id, {
                    time: 5000
                });
                window.parent.location.reload();
                parent.layer.close(index);
            }
        }
        $('.layui-btn-container .layui-btn').on('click', function () {
            var othis = $(this),
                type = othis.data('type');
            active[type] && active[type].call(this);
        });

        function cole(){
            var active = {
                close: function() {
                    parent.layer.msg('修改成功');
                    window.parent.location.reload();
                    parent.layer.close(index);
                }
            }
            $('.layui-btn-container .layui-btn').on('click', function() {
                var othis = $(this),
                    type = othis.data('type');
                active[type] && active[type].call(this);
            });
        }
    });
</script>

</html>